import {
  host,
  SERIAL
} from '../config.js'
import {
  message
} from './message.js'
export default class UploadImg {
  /**
   * [constructor description]
   * @param  {[Array]} files [chooseImg选中的tempFilePaths]
   * @param  {[Object]} json  [success每上传成功一张调用 complete全部上传完成调用]
   * @return {[void]}       [description]
   */
  constructor(files, json) {
    if (!Array.isArray(files)) {
      throw new Error('Class UploadImg parameter must be an array');
    }
    let _this = this;
    _this.data = [];
    _this.fn = json.success;
    _this.url = json.url ? host + '/api' + json.url : host + '/api' + global.apiUrls
      .UPLOAD_FILE_URL;
    _this.complete = json.complete;
    _this.files = files;
    _this.formData = json.formData || {};
    _this.fileLen = _this.files.length;
    _this.curIndex = 0;
    uni.showLoading({
      title: global.i18n.t('上传中')
    });
    _this.upload();
  }
  upload() {
    let _this = this;
    uni.uploadFile({
      url: this.url,
      filePath: this.files[this.curIndex].path,
      name: 'file',
      header: {
        'user-token': global.token || '',
        'Accept-Language': global.locale,
        'Accept-Serial': SERIAL,
      },
      formData: {
        'module': '',
        dir: "file",
        name: this.files[this.curIndex].name,
        ...this.formData
      },
      success: res => {
        let data = JSON.parse(res.data);
        if (data.code == 1) {
          console.log("返回结果", data);
          if (Array.isArray(data.data)) {
            this.data.push(data.data[0]);
          } else {
            this.data.push(data.data);
          }
          if (this.fn) this.fn(this.data);
        } else {
          message.info(global.i18n.t('图片上传失败，请重试'))
        }
      },
      complete: () => {
        this.curIndex++; // 这个图片执行完上传后，开始上传下一张
        if (this.curIndex == this.fileLen) { // 当图片传完时，停止调用
          this.complete(this.data);
          uni.hideLoading()
        } else { // 若图片还没有传完，则继续调用函数
          this.upload();
        }
      }
    });
  }
}

